//
//  ZZLightViewCellModel.m
//  ZZBlue
//
//  Created by stonesMonkey on 2017/9/23.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "ZZLightViewCellModel.h"
#import "ZZLightColorTool.h"
#define kInColor [UIColor zColorWithRed:24 green:23 blue:23]
#define kLineWidth 12
#define klineColor [UIColor zColorWithRed:52 green:52 blue:52]
@implementation ZZLightViewCellModel

- (instancetype)init {

    if (self = [super init]) {

        self.pinLv = 0.5;
        self.lightValue = 0.5;
        self.tuoWeiLength = 0;
        self.colorZuHeShu = 5;
    }
    return self;
}

- (LightModel)model {
    
    if (_model == 0) {
        
        _model = LightModelAdd;
    }
    return _model;
}

- (BOOL)isEqualModel:(ZZLightViewCellModel *)model {
    
    if (self.tuoWeiLength != model.tuoWeiLength) {
        
        return NO;
    }
    
    if (self.colorZuHeShu != model.colorZuHeShu) {
        
        return NO;
    }
    
    if (![self.title isEqualToString:model.title]) {
        
        return NO;
    }
    
    if (model.model != self.model) {
        
        return NO;
    }
    
    if(self.isAnticlockwise != model.isAnticlockwise) {
        
        return NO;
    }
    
    if (self.pinLv != model.pinLv) {
        
        return NO;
    }
    
    if (self.colorNumber != model.colorNumber) {
        
        return NO;
    }
    
    if (self.lightValue != model.lightValue) {
        
        return NO;
    }
    
    if (self.colors.count != model.colors.count) {
        
        return NO;
    }
    
    if (![self.imagePath isEqualToString:model.imagePath]) {
        return NO;
    }
    
    for (NSInteger j = 0; j < model.colorsGroup.count; j++) {
        
        NSMutableArray *colors = model.colorsGroup[j];
        for (NSInteger index = 0; index< colors.count; index++) {
            
            ZZColorRGBA *rgba = colors[index];
            ZZColorRGBA *modelRGBA = self.colorsGroup[j][index];
            if (rgba.R != modelRGBA.R || rgba.G != modelRGBA.G || rgba.B != modelRGBA.B || rgba.A != modelRGBA.A) {
                
                return NO;
            }
        }
    }

    
    
    return YES;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    
    [aCoder encodeObject:self.title forKey:@"title"];
    [aCoder encodeInteger:self.model forKey:@"model"];
    [aCoder encodeBool:self.isAnticlockwise forKey:@"isAnticlockwise"];

    [aCoder encodeInteger:self.colorNumber forKey:@"colorNumber"];
    [aCoder encodeFloat:self.tuoWeiLength forKey:@"tuoWeiLength"];
    [aCoder encodeInteger:self.colorZuHeShu forKey:@"colorZuHeShu"];
    [aCoder encodeFloat:self.pinLv forKey:@"pinLv"];
    [aCoder encodeFloat:self.lightValue forKey:@"lightValue"];
    [aCoder encodeObject:self.imagePath forKey:@"imagePath"];
    [aCoder encodeCGSize:self.imageSize forKey:@"imageSize"];
    [aCoder encodeBool:self.isSelect forKey:@"isSelect"];
    [aCoder encodeObject:self.colorsGroup forKey:@"colorsGroup"];
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        
        self.title = [aDecoder decodeObjectForKey:@"title"];
        self.model = [aDecoder decodeIntegerForKey:@"model"];
        self.isAnticlockwise = [aDecoder decodeBoolForKey:@"isAnticlockwise"];
        
        self.colorNumber = [aDecoder decodeIntegerForKey:@"colorNumber"];
        self.tuoWeiLength = [aDecoder decodeFloatForKey:@"tuoWeiLength"];
        self.colorZuHeShu = [aDecoder decodeIntegerForKey:@"colorZuHeShu"];
        self.pinLv = [aDecoder decodeFloatForKey:@"pinLv"];
        self.lightValue = [aDecoder decodeFloatForKey:@"lightValue"];
        self.imagePath = [aDecoder decodeObjectForKey:@"imagePath"];
        self.imageSize = [aDecoder decodeCGSizeForKey:@"imageSize"];
        self.isSelect = [aDecoder decodeBoolForKey:@"isSelect"];
        self.colorsGroup = [aDecoder decodeObjectForKey:@"colorsGroup"];
    }
    return self;
}

- (void)reloadImageForIndex:(NSInteger)index {
    
    switch (self.model) {
        case LightModelAdd:
        {
            
            ZZView *view = [ZZCircleTool viewForColors:@[[UIColor zColorWithRed:52 green:52 blue:52]] viewFrame:CGRectMake(0, 0, self.imageSize.width, self.imageSize.height) lineWidth:kLineWidth inCircleColor:kInColor];
            
            self.imagePath = [view image];
            
        }
            break;
        case LightModelQiCai:
        {
            
            NSInteger ledNumber = 0;
            ZZView *view = [ZZCircleTool qiCaiMoreColorViewForColors:[self nativeColors] viewFrame:CGRectMake(0, 0, self.imageSize.width, self.imageSize.height) lineColor:klineColor lineWitdh:kLineWidth inColor:kInColor ledNumber:ledNumber];
            self.imagePath = [view image];
        }
            break;
        case LightModelTaiJi:
        {
            ZZView *view = [ZZCircleTool taiJiViewForColors:[self nativeColors] viewFrame:CGRectMake(0, 0, self.imageSize.width, self.imageSize.height) lineWidth:kLineWidth inCircleColor:kInColor midColor:klineColor midColorLength:1 - self.tuoWeiLength ledNumber:0];
            
            self.imagePath = [view image];
        }
            break;
        case LightModelFengHuoLun:
        {
            
            
            ZZView *view = [ZZCircleTool fengHuoLunViewForColors:[self nativeColors] viewFrame:CGRectMake(0, 0, self.imageSize.width, self.imageSize.height) lineWidth:kLineWidth inCircleColor:kInColor tuoWeiColor:klineColor tuoWeiLength:self.tuoWeiLength ledNumber:0];
            self.imagePath = [view image];;
            
            
        }
            break;
        case LightModelFugai:
        {
            ZZView *view = [ZZCircleTool fuGaiviewForColors:[self nativeColorsForIndex:index] viewFrame:CGRectMake(0, 0, self.imageSize.width, self.imageSize.height) lineWidth:kLineWidth inCircleColor:kInColor midColor:klineColor ledNumber:0];
            self.imagePath = [view image];
        }
            break;
        case LightModelTiaoBian:
        {
            
            
            ZZView *view = [ZZCircleTool tiaobianvViewForColors:[self nativeColorsForIndex:index] viewFrame:CGRectMake(0, 0, self.imageSize.width, self.imageSize.height) lineWidth:kLineWidth inCircleColor:kInColor tuoWeiColor:klineColor ledNumber:0];
            self.imagePath = [view image];
        }
            break;
            
        default:
            break;
    }
}

- (void)reloadImage{

    switch (self.model) {
        case LightModelAdd:
        {

            ZZView *view = [ZZCircleTool viewForColors:@[[UIColor zColorWithRed:52 green:52 blue:52]] viewFrame:CGRectMake(0, 0, self.imageSize.width, self.imageSize.height) lineWidth:kLineWidth inCircleColor:kInColor];

            self.imagePath = [view image];

        }
            break;
        case LightModelQiCai:
        {

            NSInteger ledNumber = 0;
            ZZView *view = [ZZCircleTool qiCaiMoreColorViewForColors:[self nativeColors] viewFrame:CGRectMake(0, 0, self.imageSize.width, self.imageSize.height) lineColor:klineColor lineWitdh:kLineWidth inColor:kInColor ledNumber:ledNumber];
            self.imagePath = [view image];
        }
            break;
        case LightModelTaiJi:
        {
            ZZView *view = [ZZCircleTool taiJiViewForColors:[self nativeColors] viewFrame:CGRectMake(0, 0, self.imageSize.width, self.imageSize.height) lineWidth:kLineWidth inCircleColor:kInColor midColor:klineColor midColorLength:1 - self.tuoWeiLength ledNumber:0];

            self.imagePath = [view image];
        }
            break;
        case LightModelFengHuoLun:
        {


            ZZView *view = [ZZCircleTool fengHuoLunViewForColors:[self nativeColors] viewFrame:CGRectMake(0, 0, self.imageSize.width, self.imageSize.height) lineWidth:kLineWidth inCircleColor:kInColor tuoWeiColor:klineColor tuoWeiLength:self.tuoWeiLength ledNumber:0];
            self.imagePath = [view image];;


        }
            break;
        case LightModelFugai:
        {
            ZZView *view = [ZZCircleTool fuGaiviewForColors:[self nativeColors] viewFrame:CGRectMake(0, 0, self.imageSize.width, self.imageSize.height) lineWidth:kLineWidth inCircleColor:kInColor midColor:klineColor ledNumber:0];
            self.imagePath = [view image];
        }
            break;
        case LightModelTiaoBian:
        {


            ZZView *view = [ZZCircleTool tiaobianvViewForColors:[self nativeColors] viewFrame:CGRectMake(0, 0, self.imageSize.width, self.imageSize.height) lineWidth:kLineWidth inCircleColor:kInColor tuoWeiColor:klineColor ledNumber:0];
            self.imagePath = [view image];
        }
            break;

        default:
            break;
    }
}


- (void)equalToModel:(ZZLightViewCellModel *)model {
   
    self.title = model.title;
    self.model = model.model;
    self.isAnticlockwise = model.isAnticlockwise;
    self.imageSize = model.imageSize;
    self.colorNumber = model.colorNumber;
    self.tuoWeiLength = model.tuoWeiLength;
    self.colorZuHeShu =  model.colorZuHeShu;
    self.pinLv = model.pinLv;
    self.lightValue = model.lightValue;
    self.imagePath = model.imagePath;
    self.isSelect = model.isSelect;
    self.colorsGroup = model.colorsGroup;

}



- (NSArray *)outPutColors {

    if (self.model == LightModelFugai) {
        
        return self.colorsGroup;
    }
    
    return [self nativeColors];
}

//- (NSMutableArray *)outPutColors {
//
//    _outPutColors =
//
//    return _outPutColors;
//}

- (NSArray <UIColor *> *)colorForLEDNumber:(NSInteger)ledNumber {


//    switch (self.model) {
//        case LightModelQiCai:
//        {
//            return [self qicaiForLEDNumber:ledNumber];
//        }
//            case LightModelTaiJi:
//        {
//            return self taij
//        }
//
//        default:
//            break;
//    }

    return  nil;
}

#pragma mark -太极
- (NSArray *)taiJiForLEDNumber:(NSInteger)ledNumber {

    return nil;
}

#pragma mark - 七彩
- (NSArray *)qicaiForLEDNumber:(NSInteger)ledNumber {

    if (ledNumber == 0) {

        ledNumber = 36;
    }


    NSMutableArray *outputColors = [NSMutableArray array];
    NSArray *colors = [self nativeColors];
    if (colors.count >= ledNumber) { // 如果选择的颜色大于LED的数量那么每个LED灯显示这个颜色

        return [colors subarrayWithRange:NSMakeRange(0, ledNumber)];
    }

    NSInteger loopCount = ledNumber / 3;
     NSInteger yuShuLoopCount = ledNumber % 3;
    for (int i = 0 ; i < ledNumber; i++) {

        [outputColors addObject:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
    }

    NSInteger colorsCount = colors.count;

    if (colorsCount > loopCount) {

        colorsCount = loopCount;
    }

    for (NSInteger i = 0; i < 3; i++) {

        for (NSInteger j = 0; j < colorsCount; j++) {

            outputColors[j + i * loopCount] = colors[j];
        }
    }

    
    return outputColors;
}

- (NSArray *)nativeColorsForIndex:(NSInteger)index {
    
    NSMutableArray *mtbArr = [NSMutableArray array];
    for (ZZColorRGBA *rgba in self.colorsGroup[index]) {
        
        UIColor *color = [UIColor zColorWithRed:rgba.R green:rgba.G blue:rgba.B];
        [mtbArr addObject:color];
    }
    
    return [mtbArr copy];
}

- (NSArray *)nativeColors {

    NSMutableArray *mtbArr = [NSMutableArray array];
    for (ZZColorRGBA *rgba in self.colors) {

        UIColor *color = [UIColor zColorWithRed:rgba.R green:rgba.G blue:rgba.B];
        [mtbArr addObject:color];
    }

    return [mtbArr copy];
}

- (NSMutableArray<NSMutableArray<ZZColorRGBA *> *> *)colorsGroup {
    
    if (!_colorsGroup || _colorsGroup.count == 0) {
        
        _colorsGroup = [NSMutableArray array];
   
    }
    return _colorsGroup;
}



#pragma mark - set & get
- (NSMutableArray<ZZColorRGBA *> *)colors {
    
    return self.colorsGroup[0];
}



@end

